﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using IO = System.IO;
using System.Reflection;
using MyCommander.Utils;
using MyCommander.SomeEntities;
using System.Text.RegularExpressions;

namespace MyCommander.UI
{
    /// <summary>
    /// Interaction logic for FileSystemPane.xaml
    /// </summary>
    public partial class FileSystemPane : UserControl
    {
        public FileSystemPane()
        {
            InitializeComponent();
            
            InitializeOtherStuff();
        }

        readonly private FileSystemPaneData m_data = new FileSystemPaneData();

        private string m_currentPath;
        public string CurrentPath
        {
            get { return m_currentPath; }
            set { m_currentPath = value; }
        }

        void InitializeOtherStuff()
        {
            CurrentPath = Persistence.Instance.Get(Name);
            if (null == CurrentPath || string.Empty == CurrentPath)
                CurrentPath = AppDomain.CurrentDomain.BaseDirectory;

            LoadFileSystemObjectList(CurrentPath);
            

            DataContext = m_data;
        }

        void LoadFileSystemObjectList(string path)
        {
            List<IO.FileSystemInfo> _fsoList = new List<IO.FileSystemInfo>();
            IO.DirectoryInfo di = new IO.DirectoryInfo("..");
            if (isRoot(path))
            {
                _fsoList.Add(new IO.DirectoryInfo(".."));
            }
            foreach (IO.FileSystemInfo fi in di.EnumerateFileSystemInfos())
            {
                _fsoList.Add(fi);
            }
            m_data.FileSystemObjectList = _fsoList;
        }

        Regex rootPathRegex = new Regex("^[a-z]:\\$");
        bool isRoot(string path)
        {            
            if (string.IsNullOrEmpty(path))
                throw new ArgumentException("null ba?", "path");
            
            return !rootPathRegex.IsMatch(path);
        }
    }
}

namespace MyCommander.SomeEntities
{
    class FileSystemPaneData
    {
        private List<IO.FileSystemInfo> objectList = new List<IO.FileSystemInfo>();
        public List<IO.FileSystemInfo> FileSystemObjectList
        {
            get { return objectList; }
            set { objectList = value; }
        }
    }

    class FileSystemObject : IO.FileSystemInfo
    {
        public override void Delete()
        {
            throw new NotImplementedException();
        }

        public override bool Exists
        {
            get { throw new NotImplementedException(); }
        }

        public override string Name
        {
            get { throw new NotImplementedException(); }
        }
    }
}